package ajakuulauku;

import java.awt.Canvas;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;

/**
 * Canvas where game drawing takes place
 * 
 * @author Valdar
 *
 */
@SuppressWarnings("serial")
public class GUICanvas extends Canvas {
	
	/**
	 * Bufferstrategy of canvas
	 */
	BufferStrategy buffer;
	
	/**
	 * Graphics of currente bufferstrategy
	 */
	Graphics2D graphics;
	
	GUI gui;
	
	/**
	 * Initializes Canvas
	 */
	public GUICanvas() {
		setIgnoreRepaint(true);
		this.setSize(GUI.WIDTH, GUI.HEIGHT);
		gui = new GUI();
		gui.add(this);
		gui.done();
	}

	/**
	 * Draws current buffer and prepares next
	 */
	public Graphics2D display() {
		graphics.dispose();
		buffer.show();
		graphics = (Graphics2D)buffer.getDrawGraphics();
		graphics.setColor(getBackground());
		graphics.fillRect(0, 0, getWidth(), getHeight());
		return graphics;
	}

	/**
	 * Creates bufferstrategy and prepares first buffer
	 */
	public Graphics2D init() {
		createBufferStrategy(2);
		buffer = getBufferStrategy();
		graphics = (Graphics2D)buffer.getDrawGraphics();
		return graphics;
	}
}
